<?php

namespace app\manage\services;

use app\model\Admin as AdminModel;
use app\model\Log as LogModel;
use think\facade\Config;

class Login
{
    protected $model;
    protected $adminService;
    protected $logModel;

    public function __construct()
    {
        $this->model = new AdminModel();
        $this->adminService = new Admin();
        $this->logModel = new LogModel();
    }

    //登录
    public function login($params)
    {
//        if (!captcha_check($params['captcha'])) {
//            return -1;
//        }
        $adminInfo = $this->adminService->checkUserLogin($params['username']);
        if (!$adminInfo) {
            return -2;
        }
        if ($adminInfo['user_pass'] != md5($params['password'])) {
            return -3;
        }

        //生成token
        $token = signToken($adminInfo['id'], Config::get('jwt.key'), Config::get('jwt.expire'));

        //登录日志
        $this->logModel->createInfo([
            'uid' => $adminInfo['id'],
            'message' => '用户登录',
            'path' => '/manage/login',
            'content' => '登录成功，账号：' . $adminInfo['user_login'] . '登录IP：' . get_client_ip(),
            'type' => 1
        ]);

        return [
            'token' => $token,
            'expire' => Config::get('jwt.expire')
        ];
    }
}